#include "common.h"
#include "thread_pool.h"

std::atomic<bool> g_is_running(true);

void signal_handler(int sig) {
    std::cout << "catch signal" << std::endl;
    UNUSED(sig);
    g_is_running = false;
}

void task(void *arg) {
    std::cout << "arg is " << (char *)arg << std::endl;
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}

int main() {
    signal(SIGINT, signal_handler);
    signal(SIGQUIT, signal_handler);
    signal(SIGTERM, signal_handler);

    ThreadPool threadPool(4);
    //wait thread is start
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    threadPool.SubmitTask(ThreadUnit::Task{task, (void *)"111"});
    threadPool.SubmitTask(ThreadUnit::Task{task, (void *)"222"});
    threadPool.SubmitTask(ThreadUnit::Task{task, (void *)"333"});
    threadPool.SubmitTask(ThreadUnit::Task{task, (void *)"444"});
    threadPool.SubmitTask(ThreadUnit::Task{task, (void *)"555"});
    threadPool.SubmitTask(ThreadUnit::Task{task, (void *)"666"});

    while (g_is_running) {
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }

    std::cout << "main exit" << std::endl;
    return 0;
}
